package org.example.mybooksystem.Service;

import lombok.extern.slf4j.Slf4j;
import org.example.mybooksystem.Modle.BookInfo;
import org.example.mybooksystem.Modle.PageRequest;
import org.example.mybooksystem.Modle.PageResult;
import org.example.mybooksystem.enums.BookStatusEnum;
import org.example.mybooksystem.mapper.BookInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:做渐进主义的改良派,不做完没注意的速成派
 * User: XALEXC
 * Date: 2025-02-23
 * Time: 13:17
 */
@Slf4j
@Service
public class BookService {

    @Autowired
    private BookInfoMapper bookInfoMapper;

        public PageResult<BookInfo> selectBookInfoByPage(PageRequest pageRequest){
            // 再次进行判断
            if(pageRequest==null){
                return null;
            }
            // 获取总记录数
            Integer count=bookInfoMapper.count();
            // 获取当前记录
            List<BookInfo> bookInfos=bookInfoMapper.selectBookInfoByPage(pageRequest.getOffset(), pageRequest.getPageSize());
            if(bookInfos!=null&&bookInfos.size()>0){
                for(BookInfo bookInfo:bookInfos){
                    // 使用枚举类,根据code获取name
                    bookInfo.setStatusCN(BookStatusEnum.getNameByCode(bookInfo.getStatus()).getName());
                }
            }
            System.out.println("pageRequest.getOffset()="+pageRequest.getOffset());
           return new PageResult<>(bookInfos,count,pageRequest);
        }

    /**
     * 添加图书
     * @param bookInfo
     * @return
     */
        public Integer insertBookInfo(BookInfo bookInfo){
            Integer result=null;
            try{
                result=bookInfoMapper.insertBookInfo(bookInfo);

            }catch (Exception e){
                log.error("添加图书出错,e:{}",e.getMessage());
            }
            return result;
        }

        public BookInfo queryBookInfoById(Integer id){
            return bookInfoMapper.queryBookInfoById(id);
        }

    /**
     * 更新图书
     * @param bookInfo
     * @return
     */
    public Integer updateBook(BookInfo bookInfo){
            Integer result=0;
            try{
                result=bookInfoMapper.updateBook(bookInfo);
            }catch (Exception e){
                log.error("更新图书失败,e:{}",e.getMessage());
            }
            return result;
        }

        public Integer batchDelete(List<Integer> ids){
        Integer result=0;
        try{
            result=bookInfoMapper.batchDelete(ids);
        }catch (Exception e){
            log.error("更新图书失败,e:{}",e.getMessage());
        }
        return result;
        }

}
